<?php

namespace app\supplier\model\employ;

use app\common\model\employ\Poster as PosterModel;
use app\proxy\model\proxy\Region;
use app\supplier\model\supplier\Supplier;

class Poster extends PosterModel
{


    public function getList($params, $shop_supplier_id)
    {
        $model = $this;
        if (isset($params['search']) && $params['search']) {
            $model = $model->where('name', 'like', '%' . $params['search'] . '%');
        }
        if (isset($params['is_audit'])) {
            $model = $model->where('is_audit', $params['is_audit']);
        }
        // 查询列表数据
        return $model->with(["qr_code", 'bg_image'])->where('is_delete', '=', '0')
            ->where("shop_supplier_id", $shop_supplier_id)
            ->withAttr('proxy_name', function ($value, $data) {
                return Region::getProxyName($data, 0);
            })->withAttr('city_name', function ($value, $data) {
                return \app\common\model\region\Region::getMergerName($data["city_id"]);
            })
            ->append(['proxy_name', 'city_name'])
            ->order(['create_time' => 'desc'])
            ->paginate($params);
    }

    public function add($data, $shop_supplier_id)
    {
        $supplier = Supplier::detail($shop_supplier_id);
        $data["shop_supplier_id"] = $shop_supplier_id;
        $data["proxy_id"] = $supplier["proxy_id"];
        $data['app_id'] = self::$app_id;
        $data["province_id"] = $supplier['province_id'];
        $data["city_id"] = $supplier['city_id'];
        $data["is_audit"] = 0;
        return self::create($data);
    }

    /**
     * 修改
     */
    public function edit($data, $shop_supplier_id)
    {
        // 开启事务
        $this->startTrans();
        try {
            $supplier = Supplier::detail($shop_supplier_id);
            $data['app_id'] = self::$app_id;
            $data["province_id"] = $supplier['province_id'];
            $data["city_id"] = $supplier['city_id'];
            $data["is_audit"] = 0;
            $data["publish_status"] = 0;
            $data["proxy_id"] = $supplier["proxy_id"];
            if ($this['shop_supplier_id'] !== $shop_supplier_id) {
                $this->error = '您无权编辑此记录';
                return false;
            }
            $this->save($data);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }


}